//
//  MainNewsDetailsViewModel.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/4.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "MainNewsDetailsViewModel.h"

@implementation MainNewsDetailsViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.commentsArrayList = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}
/*
 首页新闻详情
 */
-(RACSignal *)MainNewsDetailsRequestpParameter:(NSString* )newsId UserId:(NSString* )userId {
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"newsId" :newsId,
                                @"userId" :userId,
                               
                                };
        
        [ScyRequestOperationManager MainNewsDetaiParametersDic:param success:^(NSDictionary *result) {
            
            NSLog(@"首页新闻详情：%@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
            
        }];
        
        return nil;
    }];
    
}
/*
 主页新闻评论列表
 */
-(RACSignal *)MainNewsFindAllDiscussRequestpParameter:(NSString* )newsId{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"newsId"  :newsId
                                };
        
        [ScyRequestOperationManager MainNewsFindAllDiscussParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                [self.commentsArrayList addObjectsFromArray:(NSMutableArray *)[MTLJSONAdapter modelsOfClass:[MainCommentsListModel class] fromJSONArray:result[@"result"] error:nil]];
                
            }

            
            NSLog(@"主页新闻评论列表：%@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
            
        }];
        
        return nil;
    }];
    
}

/*
 主页新闻添加评论
 */
-(RACSignal *)MainNewsAddDiscussRequestpParameter:(NSString* )newsId Content:(NSString* )content UserId:(NSString* )userId{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"newsId"  :newsId,
                                @"content":content,
                                @"userId":userId,
                                };
        
        [ScyRequestOperationManager MainNewsAddDiscussParametersDic:param success:^(NSDictionary *result) {
            
            NSLog(@"主页新闻添加评论：%@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
            
        }];
        
        return nil;
    }];
    
}

/*
 主页新闻点赞
 */
-(RACSignal *)MainNewsGivethumbsRequestpParameter:(NSString* )newsId UserId:(NSString* )userId{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"newsId"  :newsId,
                                @"userId":userId
                                };
        
        [ScyRequestOperationManager MainNewsGivethumbsParametersDic:param success:^(NSDictionary *result) {
            
            NSLog(@"主页新闻点赞：%@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
            
        }];
        
        return nil;
    }];
    
}

/*
 新闻收藏
 */
-(RACSignal *)SaveNewsCollectionRequestpParameter:(NSString* )userId NewsId:(NSString* )newsId{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"userId"  :userId,
                                @"newsId":newsId
                                
                                };
        
        [ScyRequestOperationManager SaveNewsCollectionParametersDic:param success:^(NSDictionary *result) {
            
            NSLog(@"新闻收藏：%@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
            
        }];
        
        return nil;
    }];
    
}


/*
 取消新闻收藏
 */
-(RACSignal *)DelNewsCollectionRequestpParameter:(NSString* )userId NewsId:(NSString* )newsId{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary *param = @{@"userId"  :userId,
                                @"newsId":newsId
                                };
        
        [ScyRequestOperationManager DelNewsCollectionParametersDic:param success:^(NSDictionary *result) {
            
            NSLog(@"取消新闻收藏：%@",result);
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
            
        } failture:^(id result) {
            
            [subscriber sendError:result];
            
        }];
        
        return nil;
    }];
    
}

@end
